
//#include "msp430x54x.h"
#include "SMBUS_Define.h"

enum Device_Enable
{
  DeviceOff,
  DeviceOn
};

///////////////////////////////////////////////////////////////////////
// Globle Variables
extern unsigned int G_Device_Action_Controls;  //initial in TimerA.c
extern unsigned int G_IIC_Active_Status;

void InitialDriversGlobleVariables();

///////////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////////
/* Device Action Control Bits , (G_Device_Action_Controls)
 * unsigned int (16 bits)   */

#define LED_BLINK    				    (0x0001)    //Led blink flag at timer b
#define CHG_MOSFET_STATUS       (0x0002)    //CHG MOSFET Status
#define DSG_MOSFET_STATUS  	    (0x0004)    //DSG MOSFET Status
#define SW_TRIGGER   				    (0x0008)    //SW Button Edge trigger status
#define ADC_CONVERSION          (0x0010)    //ADC 12 Conversion Enable                  
#define CH1_BALANCE	 				    (0x0020)    //channel 1 being balancing
#define CH2_BALANCE       	    (0x0040)    //channel 2 being balancing
#define CH3_BALANCE     		    (0x0080)    //channel 3 being balancing
#define BUTTON_CLICK            (0x0100)    //BUTTON_CLICK
#define BUTTON_LONG_PRESS       (0x0200)    //BUTTON_LONG_PRESS                                                             
#define BUTTON_PRESS				    (0x0400)    //BUTTON_PRESS ; set by button interrupt
#define CHG_MOS_TURNOFF_STABLE  (0x0800)    //CHG MosFet turn off to the stable time falg for mos fail checking
#define DSG_MOS_TURNOFF_STABLE	(0x1000)    //DSG MosFet turn off to the stable time falg for mos fail checking
#define CHG_MOS_TURNON_STABLE		(0x2000)    //CHG MosFet turn off to the stable time falg for ADC OC checking
#define DSG_MOS_TURNON_STABLE   (0x4000)    //DSG MosFet turn off to the stable time falg for ADC OC checking
//#define OCA				          (0x8000)        //Over Charged Alarm

////////////////////////////////////////////////////////////////////////////
//Timer A configuration
// interval time = ms
////////////////////////////////////////////////////////////////////////////
#define Timer_Based_1mS_Count         1054    //1ms = 1054 clk., MCLK = SMCLK = TBCLK = default DCO ~1.045MHz
#define Timer_A_Interval_Based_mS     5       // ms
#define Timer_A_CCR0                  (Timer_Based_1mS_Count * Timer_A_Interval_Based_mS)
#define MOSFET_Turn_Off_Stable_Count  1   //when mos turn off, must wait about 30~60us for stable;  MOSFET_Turn_Off_Stable_Count * timerA interval; When mosfet Turn off, start count to stable time for checking mosfet is whether failure.
#define MOSFET_Turn_On_Stable_Count   2   //when mos turn on, must wait about 80~100ms for stable and 10ms for sharp-down; MOSFET_Turn_On_Stable_Count * timerA interval; When mosfet Turn on, start count to stable time for checking ADC values is whether OC failure.
void InitTimerA();
void StopTimerA();
void ADC_Sampling_By_TimerA(unsigned char enable);
////////
/* TimerA Control Bits , (G_Device_TimerA_Controls), unsigned char (8 bits)   */
extern unsigned char G_Device_TimerA_Controls;
#define ADC_SAMPLING				(0x01)    //Start Sampling ADC Values by Timer A
#define First_ADCSampling		(0x02)    //First enter or start ADC sampleing after stop. 
#define CHG_OTA_FOR_MOS_CHK	(0x04)    //
//#define ADCFail     				(0x08)    //
//#define OCFail  		 				(0x10)    //
//#define no     		 				  (0x20)    //
//#define no     		 				  (0x40)    //
//#define no     			        (0x80)    //
////////
/* TimerA Status Bits , (G_Device_TimerA_Status), unsigned char (8 bits)   */
extern unsigned char G_Device_TimerA_Status;
//#define AbnormalFail				(0x01)    //
//#define MOSFETFail   				(0x02)    //
//#define NBPCFail     				(0x04)    //
//#define ADCFail     				(0x08)    //
//#define OCFail  		 				(0x10)    //
//#define no     		 				  (0x20)    //
//#define no     		 				  (0x40)    //
//#define no     			        (0x80)    //

////////////////////////////////////////////////////////////////////////////
//Timer B configuration
// interval time = ms
////////////////////////////////////////////////////////////////////////////
#define Timer_B_Interval_Based_mS   50      //50 ms
#define Timer_B_CCR0                (Timer_Based_1mS_Count * Timer_B_Interval_Based_mS)
void InitTimerB();
void StopTimerB();
void setDSGOverCurrentCounting(unsigned char enable);
void setCHGOverCurrentCounting(unsigned char enable);
void setBalancingCounting(unsigned char enable);
///////////////////////////////
//Watch Dog function counting Timer
void setWatchDogTimerToRunFunc(void (*watchDogPtr)(), unsigned int delay50ms_count);
void StartWatchDog();
void StopWatchDog();
void DisableWatchDogFunc();

////////
/* TimerB Control Bits , (G_Device_TimerB_Controls), unsigned char (8 bits)   */
extern unsigned char G_Device_TimerB_Controls;
//#define AbnormalFail				(0x01)    //
//#define MOSFETFail   				(0x02)    //
//#define NBPCFail     				(0x04)    //
//#define ADCFail     				(0x08)    //
//#define OCFail  		 				(0x10)    //
//#define no     		 				  (0x20)    //
//#define no     		 				  (0x40)    //
//#define no     			        (0x80)    //
////////
/* TimerB Status Bits , (G_Device_TimerB_Status), unsigned char (8 bits)   */
extern unsigned char G_Device_TimerB_Status;
//#define AbnormalFail				(0x01)    //
//#define MOSFETFail   				(0x02)    //
//#define NBPCFail     				(0x04)    //
//#define ADCFail     				(0x08)    //
//#define OCFail  		 				(0x10)    //
//#define no     		 				  (0x20)    //
//#define no     		 				  (0x40)    //
//#define no     			        (0x80)    //
////////////////////////////////////////////////////////////////////////////
//LED Display configuration
// 
////////////////////////////////////////////////////////////////////////////
#define LED_PORT1_OUT			P1OUT
#define LED_PORT1_DIR			P1DIR
#define LED_PORT1_SEL			P1SEL
#define LED_PORT1_DS			P1DS
#define LED_PORT1_REN			P1REN

#define Green_LED		BIT7	//
#define Red_LED		  BIT6	//
#define Yellow_LED  BIT5	//

#define ALL_LEDs		(BIT5 + BIT6 + BIT7)	//

#define LED_Blinking_Interval_Time    10  // 5 * 50 = 250 ms, timeing based = 50 ms.

void InitLEDDisplay();
void setLEDsOnOff(unsigned char LEDs_Name, unsigned char OnOff);
void InitialBlinkingLED();
void set_Blinking_Function(unsigned char LEDs_Bits, unsigned char OnOff);
void Blink_LEDs_For_Timer();
void Light_OnOff_LEDs_For_Timer();

////////////////////////////////////////////////////////////////////////////
//MosFET Control configuration 
// P9.7 CHG control
// P9.6 DSG control
////////////////////////////////////////////////////////////////////////////
#define MOSFET_PORT_OUT      P9OUT
#define MOSFET_PORT_DIR      P9DIR
#define MOSFET_PORT_SEL      P9SEL
#define MOSFET_CHG            BIT7
#define MOSFET_DSG            BIT6
void InitMosControl();
void setMosCHG(unsigned char enable);
void setMosDSG(unsigned char enable);
unsigned char getMosCHG();
unsigned char getMosDSG();

////////////////////////////////////////////////////////////////////////////
//Multiplexer configuration
// D0 = P10.5, D1 = P10.6, D2 = P10.7
//Multiplexer channel 1~8
////////////////////////////////////////////////////////////////////////////
enum Multiplexer_Channel_For_User_to_select
{
  mux_ch1,
  mux_ch2,
  mux_ch3,
  mux_ch4,
  mux_ch5,
  mux_ch6,
  mux_ch8,
  mux_ch7
};

#define MUX_PORT_OUT      P10OUT
#define MUX_PORT_DIR      P10DIR
#define MUX_PORT_SEL      P10SEL
#define MUX_D0            BIT5
#define MUX_D1            BIT6
#define MUX_D2            BIT7
void InitMultiplexer();
void setMuxChannel(unsigned char channel);

////////////////////////////////////////////////////////////////////////////
//  Button configuration
//  Switch P2.3, press = high, releae = low
//  
////////////////////////////////////////////////////////////////////////////
//#define BUTTON_LONG_PRESS_TIME       100 // 100 * 50 = 5000 ms = 5 sec., timeing based = 50 ms.
extern unsigned char ButtonPressCounter;

enum Button_Trigger_Setting
{
  Disable_SW_Trigger,
  SW_Rising_Edge_Trigger,
  SW_Falling_Edge_Trigger
};

#define BUTTON_PORT_IN          P2IN
#define BUTTON_PORT_DIR			    P2DIR
#define BUTTON_PORT_SEL			    P2SEL
#define BUTTON_PORT_IE          P2IE
#define BUTTON_PORT_IES         P2IES
#define BUTTON_PORT_IFG         P2IFG
#define BUTTON_PORT_VECTOR      PORT2_VECTOR
#define SW1_BUTTON     		    BIT3

void InitButton();
void Set_SW_Trigger(unsigned char button_Trigger_Setting);
unsigned char getSW1Status();

////////////////////////////////////////////////////////////////////////////
//AddressGetting configuration
// P4 for I2C Slave address
// getting address from P4.0~P4.6 (7bits), P4.7 is ignored.
////////////////////////////////////////////////////////////////////////////
#define ADDS_PORT_IN      P4IN
#define ADDS_PORT_DIR     P4DIR
#define ADDS_PORT_SEL     P4SEL
void InitAddressGetting();
unsigned char getAddress();
unsigned char getSlaveAddressChangeMode();

////////////////////////////////////////////////////////////////////////////
//Balance Control configuration
// B1 = P8.1/TA0.1, B2 = P8.2/TA0.2, B3 = P8.3/TA0.3
////////////////////////////////////////////////////////////////////////////
enum Balance_Control_Chennal_Setting
{
  Disable_Balancing,
  Ch1_Balancing,
  Ch2_Balancing,
  Ch3_Balancing
};

#define BCTRL_PORT_OUT      P8OUT
#define BCTRL_PORT_DIR      P8DIR
#define BCTRL_PORT_SEL      P8SEL
#define BCTRL_CH1            BIT1
#define BCTRL_CH2            BIT2
#define BCTRL_CH3            BIT3
#define ALL_BCTRL_CH         (BCTRL_CH1 + BCTRL_CH2 + BCTRL_CH3)

void InitBalanceControl();
void setBalanceChannel(unsigned char channel);
unsigned char isBeingBalancing();

////////////////////////////////////////////////////////////////////////////
// Adc Reader configuration
// channel A0 : System Current sense (+/-)
// channel A1 : reserved for System Current sense (+/-)
// channel A2 : reserved.
// channel A3 : Temperature 1
// channel A4 : Temperature 2
// channel A5 : Temperature 3
// channel A6 : Temperature 4
// channel A7 : Temperature 5
////////////////////////////////////////////////////////////////////////////
#define Num_of_Results      4
#define ADC_PORT_SEL        P6SEL

void InitAdcReader();
void startAdcConversion();
void stopAdcConversion();
unsigned int getSystemCurrentADC();
unsigned int getCHGCurrentADC();
unsigned int getDSGCurrentADC();
//unsigned int getESSModuleVoltage();
//unsigned int getTemp1ADC();
//unsigned int getTemp2ADC();
//unsigned int getTemp3ADC();
//unsigned int getTemp4ADC();



////////////////////////////////////////////////////////////////////////////
// Calendar configuration
// Real Time Clock Workaround 
//
////////////////////////////////////////////////////////////////////////////
//struct Time { int Year, Month, DayOfWeek, Day, Hour, Minute, Second; };

//#define _BCD_MODE_
extern int SetRTCYEAR(int year); 	
extern int SetRTCMON(int month);
extern int SetRTCDAY(int day);
extern int SetRTCDOW(int dow);
extern int SetRTCHOUR(int hour);
extern int SetRTCMIN(int min);
extern int SetRTCSEC(int sec);

extern int GetRTCTIM0(void); 	
extern int GetRTCTIM1(void); 	
extern int GetRTCDATE(void);
extern int GetRTCYEAR(void);

extern int GetRTCMON(void);
extern int GetRTCDOW(void);
extern int GetRTCDAY(void);
extern int GetRTCHOUR(void);
extern int GetRTCMIN(void);
extern int GetRTCSEC(void);
///////////////////////////////////////////////////////
///////////////////////////////////////////////////////
void setupRTC(int Year, int Month, int Day, int Hour, int Minute, int Second);
void getClockUpdate(void);
void setDailyCounterStart(void (*ptr_func)());
void DisableDailyCounter();



unsigned int getHour();
unsigned int getMin();
unsigned int getSec();

unsigned int getYear();
unsigned int getMonth();
unsigned int getDay();


//////////////////////////////////////////////////////////////////////////////
////  
////  Button configuration
////  Switch P2.3, press = high, releae = low
//////////////////////////////////////////////////////////////////////////////
//
//#define BUTTON_PORT_IN          P2IN
//#define BUTTON_PORT_DIR			    P2DIR
//#define BUTTON_PORT_SEL			    P2SEL
//#define BUTTON_PORT_IE          P2IE
//#define BUTTON_PORT_IES         P2IES
//#define BUTTON_PORT_IFG         P2IFG
//#define BUTTON_PORT_VECTOR      PORT2_VECTOR
//
//#define SW1_BUTTON     		    BIT3
////#define SW1_BUTTON     		    BIT4 // for demo board (up)
//void InitButton();
//unsigned char getSW1Status();
//void Enable_SW_Interrupt(unsigned char enable);

//
//////////////////////////////////////////////////////////////////////////////
////Timer B configuration
//////////////////////////////////////////////////////////////////////////////
//void InitTimerB();
//void setTimerInterrupt(char enable);
////void setTimerInterrupt();
////void setCCRInterrupt();
////void setTimerFunc(int (*ptr1)(char enable), char startStatus, unsigned int delay50ms_count);
////void setTimerFuncForBlink(void (*ptr1)(char enable, char leftData, char rightData),char startStatus ,char leftData, char rightData, unsigned int delay50ms_count){
//void setTimerFuncForBlink(void (*ptr1)(char enable), char startStatus, unsigned int delay50ms_count);
//void disableTimerFuncForBlink();
//void setButtonTimerToRunFun(void (*ptr1)(), unsigned int delay50ms_count);
//void setButtonTimer(unsigned int delay50ms_count);
//void disableButtonTimer();
//void setBalanceTimerToRunFunc(void (*ptr1)(), unsigned int delay50ms_count);
//void disableBalanceTimer();
//void setProtectionTimerToRunFunc(void (*ptr1)(), unsigned int delay50ms_count);
//void disableProtectionTimer();
////Over Current counting Timer
//void setOverCurrentCountingTimerToRunFunc(void (*ptr1)());
//void DisableOverCurrentCountingFunc();


//////////////////////////////////////////////////////////////////////
////////////////////Watch Dog function
//////////////////void setWatchDogTimerToRunFunc(void (*watchDogPtr)(), unsigned int delay50ms_count);
//////////////////void StartWatchDog();
//////////////////void StopWatchDog();
//////////////////void DisableWatchDogFunc();
//////////////////
//////////////////
//////////////////
////////////////////#define _TimerCounterOn_
//////////////////#ifdef _TimerCounterOn_
//////////////////void settf();
//////////////////void stoptf();
//////////////////void cleartfc();
//////////////////#endif
//////////////////







